//
//  CalclatorApp.m
//  iCalculator
//
//  Created by Stephen Houser on 9/7/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "CalculatorApp.h"
#import "CalculatorEngine.h"
#import "BasicCalculatorView.h"

@implementation MasterView

- (id) initWithFrame: (CGRect) frame {
	//NSLog(@"MasterView initWithFrame");

	self = [super initWithFrame: frame];
	views = [[NSMutableArray alloc] init];
	currentViewIndex = -1;
	
	return self;
}

- (BOOL) canHandleSwipes {
	return TRUE;
}

- (int)swipe:(int)type withEvent:(struct __GSEvent *)event {
	//NSLog(@"MasterView swipe: %d withEvent: %@", type, event);
	int transType = 0;
	
	if (type == 4) {		// kUIViewSwipeLeft
		transType = 1;
		currentViewIndex--;
		if (currentViewIndex < 0) {
			currentViewIndex = [views count] - 1;
		}
	} else if (type == 8) {	//kUIViewSwipeRight
		transType = 2;
		currentViewIndex++;
		if (currentViewIndex == [views count]) {
			currentViewIndex = 0;
		}
	}

	// This is how to slide.... save for later
	//NSLog(@"transition to view %d", currentViewIndex);
	if (transType != 0) {
		[self transition:transType toView: [views objectAtIndex: currentViewIndex]];	
	}
	
	// transition to current view
    return [ super swipe:type withEvent:event ];
}

- (void) addView: (id) fp8 {
	[views addObject: fp8];	
	if (currentViewIndex == -1) {
		[super addSubview: fp8];
		currentViewIndex = 0;
	}
}

- (void) dealloc {
	[views release];
	[super dealloc];
}
@end

@implementation CalculatorApp
/*
 * This is the command interface. All the keypressed go through here.
 * This routine then farms out to the models (engine) and other views
 * when/if needed to ensure everyone sees what is going on.
 */
- (void) handleCommand: (NSString *) command {
	//NSLog(@"CalculatorApp handleCommand: [%@]", command);
	
	// pass the commands down the chain
	[engine sendCommand: command];

	// then update all the views...
	[self updateViews];
}

/*
 * Send an update request to all the views we know about.
 * This is real cheesey right now and has the views hardcoded
 * A lst of views would be better... or even If I knew the
 * NS/Cocoa framework better there might be something better
 * to be used.
*/
- (void) updateViews {
	//NSLog(@"CalculatorApp updateViews");
	[basicCalculatorView updateView];
}

/*
 * Main entry point for the UIKit application. This gets called
 * to "start" the application from all that jazz in main.
 *
 * Herein the main windows and views are created and their user 
 * interfaced built. Then, blamo! it all gets put up on the screen
 * and the event loop takes over. Just like all good event driven 
 * applications. It's like magic.
 */
- (void) applicationDidFinishLaunching: (id) fp8 {
	// create the calculation engine, we need this later -- it does all the work.
	engine = [[CalculatorEngine alloc] init];

	// init main window for the application to be the whole screen.
	struct CGRect frame = [UIHardware fullScreenApplicationContentRect];
	frame.origin.x = frame.origin.y = 0.0f;
	mainWindow = [[UIWindow alloc] initWithContentRect: frame];

	// The "master" view that contains a stack of views that you can
	// "slide" between using UITransitionView. We just start with the
	// calculator view for now, until I get the sliding working.
	masterView = [[MasterView alloc] initWithFrame: frame];

	// The basic calculator view -- similar to Apple's -- goes in the masterView
	basicCalculatorView = [[BasicCalculatorView alloc] initWithFrame: frame withController: self withEngine: engine];
	[masterView addView: basicCalculatorView];
	
	// A simple sample navigation bar -- not "really" used just yet.
	navView = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
	[navView showButtonsWithLeftTitle: @"Decimal" rightTitle: @"Hex" leftBack: YES];
	[navView setPrompt: @"Caclulator2"];
	[navView setBarStyle: 0];
	[navView enableAnimation];
	//[masterView addView: navView];

	// A diagnostic view -- not "really" used just yet. 
	diagView = [[UITouchDiagnosticsLayer alloc] initWithFrame: frame];
	[diagView setDisplayChargeMap: true];
	[diagView setDisplayHitRects: true];
	[diagView setDisplayTouchEvents: true];
	[diagView setTrackTouchEvents: true];
	[masterView addView: diagView];
	
	// Put the lime in the coconut... and shake it all up.
	[mainWindow orderFront: self];
    [mainWindow makeKey: self];
	[mainWindow _setHidden: NO];	
	[mainWindow setContentView: masterView]; 
}

// This would be cool to have the display "fall" to one side
// of the window or the other as the person tilts the phone.
- (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz {
	NSLog(@"accelerated x=%g, y=%g, z=%g", xx, yy, zz);
}

@end
